package cn.appsys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.appsys.pojo.BackendUser;
import cn.appsys.serivce.Backend_UserSerivce;
import cn.appsys.serivce.Data_dictionarySerivce;

@Controller
@RequestMapping("/Backend")
public class BackendController {
	@Resource(name="Backend_UserSerivce")
	private Backend_UserSerivce backendUserSerivce;
	@Resource(name="Data_dictionarySerivce")
	private Data_dictionarySerivce Data_dictionarySerivce;
	
	Logger logger=Logger.getLogger(BackendController.class);
	//跳转至登录页面
	@RequestMapping("/backUserLogin")
	public String backUserLogin() {
		logger.debug("backUserLogin<><><><><>>>>");
		return "WEB-INF/jsp/backendlogin";
	}
	
	//登录 验证用户名密码 跳转至显示的页面
	@RequestMapping("/dologin")
	public String backUserLogindo(String userCode,String userPassword,Model model) {
		logger.debug("backUserLogin=====<userCode:"+userCode+">=====<userPassword:"+userPassword+">");
		boolean state=true; //条件正确
		
		//用户对象
		BackendUser backendUser= backendUserSerivce.BackendUserLogin(userCode, userPassword);
		
		//如果是空的回到登录页面
		if(userCode==""||userCode==null&&userPassword==null||userPassword=="") {
			model.addAttribute("error","用户名和密码不能为空");
			state=false;
		}else {
			if(userCode==""||userCode==null) {
				//用户名判断
				model.addAttribute("error","用户名不能为空");
				state=false;
			}
			if(userPassword==null||userPassword=="") {
				//密码判断
				model.addAttribute("error","密码不能为空");
				state=false;
			}
		}
		
		if(backendUser==null) {
			model.addAttribute("error","登录失败 用户名或密码错误");
			state=false;
		}
		
		//都是空的   只有一个是空的  读下来 剩下的就是都不是空的
		if(state==true) {
			//读不进 角色表
			backendUser.setUserTypeName("超级管理员");
//			没问题 去main.jsp页面
			model.addAttribute("userSession",backendUser);
			return "WEB-INF/jsp/backend/main";
		}else {
			//有问题回到登录页面
			return "WEB-INF/jsp/backendlogin";
		}
	}
	
	/**
	 * 退出 
	 */
	@RequestMapping("/logout")
	public String LogOut(HttpSession session) {
		session.removeAttribute("userSession");
		return "WEB-INF/jsp/backendlogin";
	}
	
	/**
	 * APP列表
	 */
	@RequestMapping("/list")
	public String appList(Model model) {
		model.addAttribute("flatFormList", Data_dictionarySerivce.getUserList());//所属平台加载
		
		return "WEB-INF/jsp/backend/applist";
	}
	
}
